package com.aceitunaproject.server.entity.installation;

import java.awt.Point;

import com.aceitunaproject.server.AceitunaContext;
import com.aceitunaproject.server.entity.BasicEntity;
import com.aceitunaproject.server.entity.unit.Unit;
import com.aceitunaproject.server.installation.InstallationType;
import com.aceitunaproject.server.installation.InstallationTypeManager;
import com.aceitunaproject.server.player.Player;
import com.sun.sgs.app.AppContext;

public class Installation extends BasicEntity {

	private static final long serialVersionUID = 1L;

	/**
	 * The type of installation (factory,farm,etc)
	 */
	private String installationTypeId;
	private boolean complete = false;
	private double manTicksInvested = 0;
	private Long buildingUnitId;
	private double lastUpdateTick;

	/**
	 * @param id
	 * @param name
	 * @param owner
	 * @param controller
	 * @param currentPosition
	 * @param buildingUnit
	 * @param complete
	 * @param installationTypeId
	 * @param manTicksInvested
	 */
	public Installation(Long id, String name, Player owner, Player controller,
			Point currentPosition, Unit buildingUnit, boolean complete,
			InstallationType installationType, double manTicksInvested) {
		super(id, name, owner, controller, currentPosition);
		this.buildingUnitId = buildingUnit.getId();
		this.complete = complete;
		this.installationTypeId = installationType.getDefaultName();
		this.manTicksInvested = manTicksInvested;

		lastUpdateTick = AceitunaContext.getTickManager().getCurrentTick();
	}

	/**
	 * Sets the installation's Type.
	 * 
	 * @param installationType
	 *            The type of installation (factory,farm,etc)
	 */
	public void setInstallationType(InstallationType installationType) {
		AppContext.getDataManager().markForUpdate(this);
		this.installationTypeId = installationType.getDefaultName();
	}

	/**
	 * Gets the installation's Type.
	 * 
	 * @return The type of installation (factory,farm,etc)
	 */
	public InstallationType getInstallationType() {

		InstallationTypeManager man = AceitunaContext
				.getInstallationTypeManager();
		return man.getAllInstallationTypes().get(this.installationTypeId);
	}

	/**
	 * @param true if the installation is complete, false if its not
	 */
	public void setComplete(boolean complete) {
		AppContext.getDataManager().markForUpdate(this);
		this.complete = complete;
	}

	/**
	 * @return true if the installation is complete, false if its not
	 */
	public boolean isComplete() {
		if (complete == false) {
			if (buildingUnitId != null) {
				double tick = AceitunaContext.getTickManager().getCurrentTick();
				Unit unit = AceitunaContext.getUnitManager().getUnit(
						buildingUnitId);
				long persons = unit.getPersons();
				manTicksInvested = persons * (tick - lastUpdateTick);
				lastUpdateTick = tick;
				if (manTicksInvested >= getInstallationType().getManTicks()) {
					complete = true;
				}
			}
		}
		return complete;
	}

	/**
	 * @param the
	 *            number of "man/ticks" (persons * ticks) invested in the
	 *            building of this installation.
	 */
	public void setManTicksInvested(int manTicksInvested) {
		AppContext.getDataManager().markForUpdate(this);
		this.manTicksInvested = manTicksInvested;
	}

	/**
	 * @return the number of "man/ticks" (persons * ticks) invested in the
	 *         building of this installation.
	 */
	public double getManTicksInvested() {
		return manTicksInvested;
	}

	/**
	 * @param buildingUnit
	 *            the buildingUnit to set
	 */
	public void setBuildingUnit(Unit buildingUnit) {
		AppContext.getDataManager().markForUpdate(this);
		if (buildingUnit == null) {
			buildingUnitId = null;
		} else {
			this.buildingUnitId = buildingUnit.getId();
		}
	}

	/**
	 * @return the buildingUnit
	 */
	public Unit getBuildingUnit() {
		return AceitunaContext.getUnitManager().getUnit(buildingUnitId);
	}

}
